package com.glory.dmegcpda.view

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.view.View
import androidx.fragment.app.FragmentTransaction
import com.glory.dmegcpda.R
import com.glory.dmegcpda.fragment.EqpTextFragment
import com.glory.dmegcpda.fragment.GtTextFragment
import com.glory.dmegcpda.fragment.QlTextFragment
import com.glory.dmegcpda.viewmodel.HomeViewModel
import com.glory.mvvmnet.base.BaseActivity
import kotlinx.android.synthetic.main.activity_home.*


class HomeActivity() : BaseActivity<HomeViewModel>(), View.OnClickListener, Parcelable {

    var gtTextFragment: GtTextFragment? = null
    var qlTextFragment: QlTextFragment? = null
    var eqpTextFragment: EqpTextFragment? = null

    constructor(parcel: Parcel) : this() {

    }

    override fun layoutId(): Int {
        return R.layout.activity_home
    }

    override fun initView(savedInstanceState: Bundle?) {
        gtText.setOnClickListener(this)
        qlText.setOnClickListener(this)
        eqpText.setOnClickListener(this)
    }

    override fun initData() {

        val beginTransaction = supportFragmentManager.beginTransaction()
        selected()
        gtText.setSelected(true)
        if(gtTextFragment==null){
            gtTextFragment  = GtTextFragment()
            beginTransaction.add(R.id.fragment_container, gtTextFragment!!)
        }else{
            beginTransaction.show(gtTextFragment!!)
        }
        beginTransaction.commit()
    }

    @SuppressLint("CommitTransaction")
    override fun onClick(view: View?) {

        val beginTransaction = supportFragmentManager.beginTransaction()
        hideAllFragment(beginTransaction)
        when(view){
            gtText ->{
                selected()
                gtText.setSelected(true)
                if(gtTextFragment==null){
                    gtTextFragment = GtTextFragment()
                    beginTransaction.add(R.id.fragment_container, gtTextFragment!!)
                }else{
                    beginTransaction.show(gtTextFragment!!)
                }
            }
            qlText ->{
                selected()
                qlText.setSelected(true)
                if(qlTextFragment==null){
                    qlTextFragment = QlTextFragment()
                    beginTransaction.add(R.id.fragment_container, qlTextFragment!!)
                }else{
                    beginTransaction.show(qlTextFragment!!)
                }
            }
            eqpText ->{
                selected()
                eqpText.setSelected(true)
                if(eqpTextFragment==null){
                    eqpTextFragment = EqpTextFragment()
                    beginTransaction.add(R.id.fragment_container, eqpTextFragment!!)
                }else{
                    beginTransaction.show(eqpTextFragment!!)
                }
            }
        }
        beginTransaction.commit()
    }

    fun selected() {
        gtText.setSelected(false)
        qlText.setSelected(false)
        eqpText.setSelected(false)
    }

    //隐藏所有Fragment
    fun hideAllFragment(transaction: FragmentTransaction) {
        if (gtTextFragment != null) {
            transaction.hide(gtTextFragment!!)
        }
        if (qlTextFragment != null) {
            transaction.hide(qlTextFragment!!)
        }
        if (eqpTextFragment != null) {
            transaction.hide(eqpTextFragment!!)
        }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {

    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<HomeActivity> {
        override fun createFromParcel(parcel: Parcel): HomeActivity {
            return HomeActivity(parcel)
        }

        override fun newArray(size: Int): Array<HomeActivity?> {
            return arrayOfNulls(size)
        }
    }

}
